﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace AugustVisual
{
    public class AugustNormalStackPanel : Panel
    {
        static AugustNormalStackPanel()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(AugustNormalStackPanel), new FrameworkPropertyMetadata(typeof(AugustNormalStackPanel)));
        }
        ///StackPanelb
        //VirtualizingStackPanel
        AugustDataView augustDataView = null;
        public AugustNormalStackPanel()
        {
           
        }

        protected override Size MeasureOverride(Size availableSize)
        {
            Size size = new Size();
            foreach (UIElement uIElement in Children)
            {
                uIElement.Measure(availableSize);
                Size desiredSize = uIElement.DesiredSize;
                if (false)
                {
                    size.Width += desiredSize.Width;
                    size.Height = Math.Max(size.Height, desiredSize.Height);
                }
                else
                {
                    size.Width = Math.Max(size.Width, desiredSize.Width);
                    size.Height += desiredSize.Height;
                }
            }
            return size;
        }

        protected override Size ArrangeOverride(Size arrangeSize)
        {
            Rect finalRect = new Rect(arrangeSize);
            double num = 0.0d;
            foreach (UIElement child in Children)
            {
                UIElement uIElement = child;
                if (uIElement != null)
                {
                    if (false)
                    {
                        finalRect.X += num;
                        num = finalRect.Width = uIElement.DesiredSize.Width;
                        finalRect.Height = Math.Max(arrangeSize.Height, uIElement.DesiredSize.Height);
                    }
                    else
                    {
                        finalRect.Y += num;
                        num = finalRect.Height = uIElement.DesiredSize.Height;
                        finalRect.Width = Math.Max(arrangeSize.Width, uIElement.DesiredSize.Width);
                    }
                    uIElement.Arrange(finalRect);
                }
            }
            return arrangeSize; // Returns the final Arranged size
        }

        public ItemCollection Items
        {
            get
            {
                if (augustDataView == null)
                {
                    augustDataView = FrameworkElementHelper.FindParent<AugustDataView>(this, 1, null);
                }
                return augustDataView.Items;
            }
        }
    }

}
